/**
 * 
 */
package persistence.DAO.SQL.entities;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import metier.Article;
import metier.Bean;
import metier.CategorieForum;
import persistence.ConnectionManager;
import persistence.DAO.DAO;
import persistence.DAO.DAOSQL;

/**
 * @author hozakan
 *
 */
public class DAOSQLCategorieForum extends DAOSQL {

	private static DAO instance;
	private static final Logger LOGGER = Logger.getLogger(DAOSQLCategorieForum.class);
		
	public static DAO getInstance() {
		if (instance == null)
			instance = new DAOSQLCategorieForum();
		return instance;
	}
	
	private DAOSQLCategorieForum() {
		try {
			insert = prepareInsert();
			update = prepareUpdate();
			delete = prepareDelete();
			select = prepareSelect();
		    selectAll = prepareSelectAll();		
		} catch (Exception e) {
			LOGGER.error("Erreur lors de la création des PreparedStatement");
		}
	}


	private boolean create(Bean bean)
	{
		boolean retour = false;
		CategorieForum cat = (CategorieForum) bean;
		try {
			ConnectionManager.getInstance().getConnection();
			insert.clearParameters();
			insert.setInt(1, cat.getIdForum());
			insert.setString(2, cat.getTitre());
			insert.setString(3, cat.getDescription());
			insert.executeUpdate();
			retour = true;
		} catch (SQLException e) {
			LOGGER.error("Erreur lors de la création d'une catégorie");
		} catch (ClassNotFoundException e) {
			LOGGER.error("Impossible d'obtenir la connexion");
		}		
		return retour;
	}

	private boolean update(Bean bean) {
		boolean retour = false;
		CategorieForum cat = (CategorieForum) bean;
		try {
			ConnectionManager.getInstance().getConnection();
			update.clearParameters();
			update.setInt(1, cat.getIdForum());
			update.setString(2, cat.getTitre());
			update.setString(3, cat.getDescription());
			update.setInt(4, cat.getId());
			update.executeUpdate();
			retour = true;
		} catch (SQLException e) {
			LOGGER.error("Erreur lors de la modification d'une catégorie");
		} catch (ClassNotFoundException e) {
			LOGGER.error("Impossible d'obtenir la connexion");
		}		
		return retour;
	}
	
	
	@Override
	public boolean effacer(Bean bean) {
		boolean retour = false;
		try {
			ConnectionManager.getInstance().getConnection();
			delete.clearParameters();
			delete.setInt(1, bean.getId());
			delete.executeUpdate();
			retour = true;
		} catch (SQLException e) {
			LOGGER.error("Erreur lors de la suppression d'une catégorie");
		} catch (ClassNotFoundException e) {
			LOGGER.error("Impossible d'obtenir la connexion");
		}		
		return retour;
	}

	@Override
	public boolean enregistrer(Bean bean) {
		boolean retour = false;
		if (bean.getId() == 0) {
			retour = create(bean);
		} else {
			retour = update(bean);
		}
		return retour;
	}

	@Override
	public List<Bean> getBeans() {
		ResultSet rs = null;
		List<Bean> beans = new ArrayList<Bean>();
		try {
			ConnectionManager.getInstance().getConnection();
			rs = selectAll.executeQuery();
			while (rs.next()) {
				CategorieForum cat = new CategorieForum();
				cat.setId(Integer.parseInt(rs.getString("idCategorieForum")));
				cat.setIdForum(Integer.parseInt(rs.getString("idForum")));
				cat.setDescription(rs.getString("description"));
				cat.setTitre(rs.getString("titre"));
				beans.add(cat);
			}
		} catch (SQLException e) {
			LOGGER.error("Erreur lors de la préparation du PreparedStatement");
		} catch (ClassNotFoundException e) {
			LOGGER.error("impossible d'obtenir la connexion");
		}
		return beans;
	}

	@Override
	public Bean selectionner(int id) {
		ResultSet rs = null;
		Bean bean = null;
		try {
			ConnectionManager.getInstance().getConnection();
			rs = selectAll.executeQuery();
			while (rs.next()) {
				CategorieForum cat = new CategorieForum();
				cat.setId(Integer.parseInt(rs.getString("idArticle")));
				cat.setIdForum(Integer.parseInt(rs.getString("idForum")));
				cat.setDescription(rs.getString("description"));
				cat.setTitre(rs.getString("titre"));
				bean = cat;
			}
		} catch (SQLException e) {
			LOGGER.error("Erreur lors de la préparation du PreparedStatement");
		} catch (ClassNotFoundException e) {
			LOGGER.error("impossible d'obtenir la connexion");
		}
		return bean;
	}

	@Override
	protected PreparedStatement prepareDelete() throws SQLException, ClassNotFoundException {
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("DELETE FROM categorie WHERE idCategorieForum=?");
		return ps;
	}

	@Override
	protected PreparedStatement prepareInsert()  throws SQLException, ClassNotFoundException {
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("INSERT INTO categorie " +
				"(idForum, titre, description) VALUES (?, ?, ?)");
		return ps;
	}

	@Override
	protected PreparedStatement prepareUpdate()  throws SQLException, ClassNotFoundException {
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("UPDATE categorie SET " +
				"idForum=?, titre=?, description=? WHERE idCategorieForum=?");
		return ps;
	}

	@Override
	protected PreparedStatement prepareSelect() throws SQLException,
			ClassNotFoundException {
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("SELECT * FROM categorie WHERE idCategorieForum=?");
		return ps;
	}

	@Override
	protected PreparedStatement prepareSelectAll() throws SQLException,
			ClassNotFoundException {
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("SELECT * FROM categorie");
		return ps;
	}
	
}
